package com.caowei.weicimpress.weicimpress;

import android.app.Application;
import android.content.Context;
import android.os.AsyncTask;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Created by caoweiPc on 2014/7/21.
 */
public class MyApplication extends Application {
    //后台任务
    private  List<AsynTaskBean> asynTaskBeans=new CopyOnWriteArrayList<AsynTaskBean>();
    private static Context mContext;
    private static MyApplication application;



    //本应用中启动的所有异步任务 程序退出 时要清理
    private static List<AsyncTask> asyncTasks=new CopyOnWriteArrayList<AsyncTask>();
    //异步线程 池
    private static ScheduledExecutorService executorService;

    public  List<AsynTaskBean> getAsynTaskBeans() {
        return asynTaskBeans;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext=getApplicationContext();
        asynTaskBeans=new ArrayList<AsynTaskBean>();
        application=this;
        executorService= Executors.newScheduledThreadPool((Utils.CORESIZE>4)?Utils.CORESIZE/2:
                Utils.CORESIZE);
        ThreadUncaughtExceptionHandler crashHandler = ThreadUncaughtExceptionHandler.getInstance();
// todo 以后去注释       crashHandler.init(mContext);
    }

    public  void addAsyncTaskBean(AsynTaskBean bean){
        asynTaskBeans.add(bean);
    }

    public void removeAsyncTaskBean(AsyncTask task){
        for(AsynTaskBean bean:asynTaskBeans){
            if(bean.getTask().equals(task)){
                asynTaskBeans.remove(bean);
                break;
            }
        }
    }

    public  void clear(){
        for (AsynTaskBean bean:asynTaskBeans){
            if(bean.task!=null&&!bean.task.getStatus().equals(AsyncTask.Status.FINISHED)){
                bean.task.cancel(true);
            }
        }
        asynTaskBeans.clear();
        asynTaskBeans=null;
        for(AsyncTask task:asyncTasks){
            task.cancel(true);
        }
        asyncTasks.clear();
        asyncTasks=null;
        if(executorService!=null&&!executorService.isShutdown()){
            executorService.shutdownNow();
        }
        asynTaskBeans=null;
    }

    public static ScheduledExecutorService getExecutorService() {
        return executorService;
    }

    public static void setExecutorService(ScheduledExecutorService executorService) {
        MyApplication.executorService = executorService;
    }

    public static List<AsyncTask> getAsyncTasks() {
        return asyncTasks;
    }
    public static Context getGlobalContext(){
        return mContext;
    }
    public static MyApplication getMyApplication(){
        return application;
    }
    public static class AsynTaskBean{
        private String asyntaskName;
        private AsyncTask task;
        //任务总执行时间
        private long alltime=-1;
        //任务已执行时间
        private long time=-1;

        public String getAsyntaskName() {
            return asyntaskName;
        }

        public void setAsyntaskName(String asyntaskName) {
            this.asyntaskName = asyntaskName;
        }

        public AsyncTask getTask() {
            return task;
        }

        public void setTask(AsyncTask task) {
            this.task = task;
        }

        public long getAlltime() {
            return alltime;
        }

        public void setAlltime(long alltime) {
            this.alltime = alltime;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }
    }
}
